package com.group3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.dao.PunchRecordMapper;
import com.group3.domain.PunchRecord;

import com.group3.domain.PunchRecordExample;
import com.group3.pojo.Page;
import com.group3.service.StudentPunchRecordService;
import com.group3.utils.NotNull;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * StudentPunchRecordImpl
 *
 * @author HY
 * 2021/06/10 16:54
 **/


@Service
public class StudentPunchRecordImpl implements StudentPunchRecordService {
    @Resource
    private PunchRecordMapper recordMapper;

    @Override
    public Result<List<PunchRecord>> getPunchRecord(
            String studentId, Date startTime, Date endTime,Page page) {
        PunchRecordExample recordExample = new PunchRecordExample();
        PunchRecordExample.Criteria criteria = recordExample.createCriteria();
        criteria.andStudentIdEqualTo(studentId);
        if (NotNull.judge(startTime)) {
            criteria.andPunchTimeGreaterThanOrEqualTo(startTime);
        }
        if (NotNull.judge(endTime)) {
            criteria.andPunchTimeLessThanOrEqualTo(endTime);
        }
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());//启动分页
        List<PunchRecord> punchRecords = recordMapper.selectByExample(recordExample);
        PageInfo<PunchRecord> recordPageInfo = new PageInfo<>(punchRecords);//获取分页信息

        return ResultUtil.success(punchRecords, recordPageInfo.getTotal());
    }
}
